package com.surekam.core.pdfbook.book;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

public class CreateBookPdf {
	public static void main(String[] args) {
		
	}
	
	public void createPdf(File sourceFile) throws Exception {
		System.setProperty("javax.xml.transform.TransformerFactory",
				"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
//		String inputFile = "h:/test/html/README.html";
		String outputFile = "firstdoc.pdf";
		File file = new File(outputFile);
		if (file.exists()) {
			System.out.println("文件已经存在，删除文件");
			file.delete();
			System.out.println("文件删除成功");
		}
		OutputStream os = new FileOutputStream(outputFile);
		ITextRenderer renderer = new ITextRenderer();

		renderer.setDocument(sourceFile);

		// 解决中文支持问题
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

		// 解决图片的相对路径问题
		renderer.getSharedContext().setBaseURL("file:/h:/test/html/");

		renderer.layout();

		renderer.createPDF(os);
		os.close();
		System.out.println("生成成功");
	}

}
